package xyz.gzdxc.common.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xyz.gzdxc.common.mysql.repository.UserTokenRepository;
import xyz.gzdxc.common.service.UserPrivService;
import xyz.gzdxc.common.util.ResultBuilder;
import xyz.gzdxc.common.util.UploadUtil;
import xyz.gzdxc.common.vo.response.ResultVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    UserTokenRepository userTokenRepository;

    @Autowired
    UserPrivService userPrivService;

    @RequestMapping("/mytest")
    public ResultVo mytest(){
        System.out.println("ttttttsssssssss........");
        List<String> privList = userTokenRepository.getPrivListByUserId(2l);
        System.out.println("privList...................." + privList);

        System.out.println(userPrivService.hasPriv(1l, "aaa"));
        System.out.println(userPrivService.hasPriv(1l, "systemManage"));
        System.out.println(userPrivService.hasPriv(1l, "userManage"));
        return ResultBuilder.success();
    }

    @RequestMapping("/testdownload")
    public String testdownload(HttpServletRequest request, HttpServletResponse response){
        String filepath = "C:\\Users\\HUAWEI\\Desktop\\testdownload.txt";
        UploadUtil.download(response, filepath);
        return "success";
    }

    @RequestMapping("/testUpload")
    public String testUpload(HttpServletRequest request, HttpServletResponse response){
        System.out.println("come here...");
        Enumeration paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements()){
            String paramName = (String)paramNames.nextElement();
            System.out.println("paramName => " + paramName);
        }

        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> multipartFiles = multipartHttpServletRequest.getFiles("uploadfile");
        for (MultipartFile multipartFile:
                multipartFiles) {
            System.out.println(multipartFile.getResource().getFilename());
        }

        UploadUtil.upload("C:\\Users\\HUAWEI\\Desktop\\ddd", multipartFiles);

        return "success";
    }

}
